package com.jxy.bean.attribute;

import com.jxy.bean.ClassFile;
import com.jxy.tool.Tool;

import javax.xml.bind.DatatypeConverter;
import java.io.DataInputStream;
import java.util.Arrays;

public class AttributeCodeInfo extends AttributeInfo {
    private int max_stack;
    private int max_locals;
    private int code_length;
    private byte[] codes;
    private AttibuteCodeExceptionInfo[] exceptions;
    private Attribute[] attributes;

    public AttributeCodeInfo(DataInputStream dis, ClassFile cf) {
        super(dis, cf);
        max_stack = Tool.readU2(dis);
        max_locals = Tool.readU2(dis);
        code_length = Tool.readU4(dis);
        codes = Tool.readBytes(dis, code_length);
        exceptions = Tool.readException(dis, cf);
        attributes = Tool.readAttribute(dis, cf);
    }

    public byte[] getCodes() {
        return codes;
    }

    @Override
    public String toString() {
        return "AttributeCodeInfo{" +
                "max_stack=" + max_stack +
                ", max_locals=" + max_locals +
                ", code_length=" + code_length +
                ", codes=" + DatatypeConverter.printHexBinary(codes) +
                ", exceptions=" + Arrays.toString(exceptions) +
                ", attributes=" + Arrays.toString(attributes) +
                '}';
    }
}
